package com.situ.company.project.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.situ.company.project.model.ProjectModel;
import com.situ.company.util.JDBCUtil;

public class ProjectDao {

	/**
	 * 添加
	 * 
	 * @param model
	 * @return
	 */
	public Integer insert(ProjectModel model) {
		String sql = "insert into project(code,name,time)values(?,?,?)";
		return JDBCUtil.update(sql.toString(), model.getCode(), model.getName(), model.getTime());
	}

	/**
	 * 删除
	 * 
	 * @param model
	 * @return
	 */
	public Integer delete(ProjectModel model) {
		StringBuffer sql = new StringBuffer("delete from project");
		List<Object> vals = appendWhere(sql, model);
		return JDBCUtil.update(sql.toString(), vals);
	}

	/**
	 * 修改
	 * 
	 * @param model
	 * @return
	 */
	public Integer update(ProjectModel model) {
		StringBuffer sql = new StringBuffer("update ");
		sql.append("project");
		List<Object> vals = appendSet(sql, model);
		return JDBCUtil.update(sql.toString(), vals);
	}

	/**
	 * 修改拼接
	 * 
	 * @param sql
	 * @param model
	 * @return
	 */
	private List<Object> appendSet(StringBuffer sql, ProjectModel model) {
		sql.append(" set id=id ");
		List<Object> objs = new ArrayList<>();
		String name = model.getName();
		if (name != null) {
			sql.append(" ,name = ? ");
			objs.add(name);
		}
		String time = model.getTime();
		if (time != null) {
			sql.append(" ,time = ? ");
			objs.add(time);
		}
		sql.append(" where code = ?");
		objs.add(model.getCode());
		return objs;
	}

	/**
	 * sql语句拼接
	 * 
	 * @param sql
	 * @param model
	 * @return
	 */
	private List<Object> appendWhere(StringBuffer sql, ProjectModel model) {
		sql.append(" where 1=1 ");
		List<Object> objs = new ArrayList<>();
		String code = model.getCode();
		if (code != null && !code.trim().isEmpty()) {
			sql.append(" and code like ? ");
			objs.add(code);
		}
		String name = model.getName();
		if (name != null && !name.trim().isEmpty()) {
			sql.append(" and name like ? ");
			objs.add(name);
		}
		String time = model.getTime();
		if (time != null && !time.trim().isEmpty()) {
			sql.append(" and time like ? ");
			objs.add(name);
		}
		String order = model.getOrderby();
		if (order != null && !order.trim().isEmpty()) {
			sql.append(" order by ").append(order);
		}
		if (model.isPageOn()) {
			sql.append(" limit ?,? ");
			objs.add(model.getRowStart());
			objs.add(model.getRowCount());
		}
		return objs;
	}

	/**
	 * 查询
	 * 
	 * @param model
	 * @return
	 */
	public List<ProjectModel> selectList(ProjectModel model) {
		StringBuffer sql = new StringBuffer("select code,name,time from project");
		List<Object> vals = appendWhere(sql, model);
		ResultSet rs = null;
		Connection conn = null;
		PreparedStatement ps = null;
		List<ProjectModel> list = new ArrayList<>();
		try {
			conn = JDBCUtil.getConnertion();
			ps = conn.prepareStatement(sql.toString());
			for (int i = 0; i < vals.size(); i++)
				ps.setObject(i + 1, vals.get(i));
			rs = ps.executeQuery();
			while (rs.next()) {
				ProjectModel mdb = new ProjectModel();
				mdb.setCode(rs.getString("code"));
				mdb.setName(rs.getString("name"));
				mdb.setTime(rs.getString("time"));
				list.add(mdb);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(conn, ps, rs);
		}
		return list;

	}

	/**
	 * 查询记录条数
	 * 
	 * @param model
	 * @return
	 */
	public int selectCount(ProjectModel model) {
		StringBuffer sql = new StringBuffer("select count(id) from project");
		List<Object> vals = appendWhere(sql, model);
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = JDBCUtil.getConnertion();
			ps = conn.prepareStatement(sql.toString());
			for (int i = 0; i < vals.size(); i++)
				ps.setObject(i + 1, vals.get(i));
			rs = ps.executeQuery();
			if (rs.next())
				return rs.getInt(1);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(conn, ps, rs);
		}
		return 0;
	}

}
